using System;
using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using UnityEngine.UI;

namespace ProjectSurvival
{
    public partial class FloatingTextController : ViewController
    {
        public static FloatingTextController Instance;

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
            }
        }

        private void OnDestroy()
        {
            Instance = null;
        }

        void Start()
        {
            FloatingText.Hide();
        }

        public static void PlayText(Vector2 position, string content, bool critical = false)
        {
            var floatingText = Instance.FloatingText.InstantiateWithParent(Instance.transform)
                .Position(position.x, position.y)
                .Self(self =>
                {
                    float posY = position.y;
                    var textTransform = self.transform.Find("Text");
                    var textComponent = textTransform.GetComponent<Text>();
                    textComponent.text = content;
                    
                    //如果暴击则把字体颜色改为红色
                    if (critical)
                    {
                        textComponent.color = Color.red;
                    }
                    else
                    {
                        textComponent.color = Color.white;
                    }

                    ActionKit.Sequence()
                        .Lerp(0, 0.5f, 0.5f, (progress) =>
                        {
                            self.PositionY(posY + progress * 0.2f);
                            textComponent.LocalScaleX(Mathf.Clamp01(progress * 4));
                            textComponent.LocalScaleY(Mathf.Clamp01(progress * 4));
                        })
                        .Delay(0.5f)
                        .Lerp(1.0f, 0, 0.3f, (progress) => { textComponent.ColorAlpha(progress); },
                            () => { textTransform.DestroyGameObjGracefully(); })
                        .Start(textComponent);
                })
                .Show();
        }
    }
}